<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 14-7-30
  Time: 下午1:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%@include file="../../../includes/common.jsp" %>
    <title>
        新增试卷
    </title>
</head>
<body>
<div id="mainContent" class="easyui-panel" data-options="fit:true" style="padding: 10px">
    <form class="form-inline" action="">

        <label for="title">题目</label>
        <input type="text" class="input-medium" name="title" id="title" value=""/>

        <label for="knowledgePoint">知识点</label>
        <input type="text" name="knowledgePoint" id="knowledgePoint" value="" class="input-medium"/>

        <label for="taskType">试题类型</label>
        <input type="text" name="taskType" id="taskType" value="" class="input-medium" flag="task_type"
               autocomplete="off"/>
        <input type="hidden" name="taskTypeId" id="taskTypeId" value="" autoHidden="autoHidden">

        <a class="btn btn-primary" onclick="query()">查询</a>
        <a class="btn btn-primary" onclick="addChecked()">添加试题</a>
    </form>
    <table id="dataTable" style=""></table>
    <div id="examination"
         style="background-color: #EEE8AA;margin-right: auto;margin-left: auto; width:90%; ;height: 70%">
        <form class="form-inline" style="margin-top: 10px" action="">
            <label for="examinationName">试卷名称</label>
            <input type="text" class="input-medium" name="examinationName" id="examinationName"
                   value="${examinationInfo.examinationName}"/>

            <label for="examinationType">试卷类型</label>
            <input type="text" name="examinationType" id="examinationType" value="${examinationInfo.showExaminationType}" class="input-medium" flag="typeValues"
                   autocomplete="off" />
            <input type="hidden" name="examinationTypeId" id="examinationTypeId" value="${examinationInfo.examinationType}" autoHidden="autoHidden">

            <a class="btn btn-primary" onclick="addExamination()">保存试题</a>
        </form>

        <c:forEach var="typeValue" items="${type}">
            <div id="${typeValue.dictCode}" name="${typeValue.dictCode}"></div>
        </c:forEach>
    </div>
    <div id="dataEdit" closed="true" modal="true" style="overflow: hidden">
        <iframe style="width: 100%;height: 100%"
                id="contentFrame"
                name="contentFrame"
                marginheight="0"
                marginwidth="0"
                frameborder="0" src=""
                allowTransparency="true">
        </iframe>
    </div>
</div>
</body>
</html>

<script type="text/javascript">
    function query() {
        listOption.url = basePath + "task/library/listAllLibraryTask";
        listOption.data = "title=" + $("#title").val().trim() + "&knowledgePoint=" + $("#knowledgePoint").val() + "&taskType=" + $("#taskTypeId").val();
        listOption.pageNumber = 1;
        loadData(listOption);
    }

    function selectAll() {
        if ($("#checkAll").attr("checked") == "checked") {
            $("[name='auditCheckBox']").attr("checked", true);
        } else {
            $("[name='auditCheckBox']").attr("checked", false);
        }
    }

    function initDataGrid() {
        $('#dataTable').datagrid({
            title: "题库",
            pagination: true,
            fitColumns: true,
            columns: [
                [
                    {field: 'id', title: "&nbsp<input id='checkAll' type='checkbox' onclick='selectAll()'/>", align: 'center', width: 200},
                    {field: 'title', title: '题目', align: 'center', width: 200},
                    {field: 'showTaskType', title: '试题类型', align: 'center', width: 200},
                    {field: 'knowledgePoint', title: '知识点', align: 'center', width: 200},
                    {field: 'upDate', title: '上传时间', sortable: true, align: 'center', width: 200},
                    {field: 'operate', title: '操作', align: 'center', width: 200}
                ]
            ],
            sortName: "",
            sortOrder: "",
            onSortColumn: onSortColumn
        });
    }

    function onSortColumn(sortColumn, sortDirection) {
        switch (sortColumn) {
            case "upDate":
                sortColumn = "up_Date";
                break;
        }
        onSortColumnDefault(sortColumn, sortDirection);
    }

    function format(data) {
        data.resultList = queryFormat(data.resultList);
        return data;
    }

    function queryFormat(temp) {
        var rowDataList = [];
        for (var i in temp) {
            idBtn = "&nbsp;<input type='checkbox' name='libraryTaskCheckBox' id='" + temp[i].id + "' value='" + temp[i].id + "'\">&nbsp;";
            editBtn = "&nbsp;<a onclick='addExaminationToCache(" + temp[i].id + ")'>添加</a>&nbsp;"
            temp[i].id = idBtn;
            temp[i].operate = editBtn;
            rowDataList.push(temp[i]);
        }
        return rowDataList;
    }

    function delTask(taskId) {
        $.messager.confirm("<spring:message code="prompt"/>", "<spring:message code="ensure"/><spring:message code="delete"/><spring:message code="questionMark"/>", function (r) {
            if (r) {
                $.ajax({
                    url: basePath + "task/examination/deleteLibraryTask?id=" + taskId,
                    dataType: "json",
                    type: "DELETE",
                    success: function () {
                        $.messager.alert("<spring:message code="succeed"/>", "<spring:message code="delete"/><spring:message code="succeed"/>", "", function () {
                            query();
                        })
                    },
                    error: doError
                })
            }
        })
    }

    function addChecked() {
        var ids = "";
        var value;
        var allBox = $("[name='libraryTaskCheckBox'] :checked");
        for (var i = 0; i < allBox.length; i++) {
            value = allBox[i].getAttribute("value");
            ids += value + ",";
        }
        ids = ids.substring(0, ids.length - 1);
        addExaminationToCache(ids);
    }

    function addExaminationToCache(ids) {
        $.ajax({
            url: basePath + "task/examination/addExaminationTaskToCache?taskIds=" + ids,
            dataType: "json",
            type: "POST",
            success: function (info) {
                $.messager.alert("成功", "添加成功！", "", function () {
                    for (var i = 0; i < info.length; i++) {
                        if (info[i].taskType == 1) {
                            $("#" + info[i].taskType).append("<div name='task'  id='t" + info[i].id + "'>");
                            $("#" + info[i].taskType).append("<p name=" + info[i].id + ">" + info[i].title + "(答案：<textarea id='answer' style='width: 40px;height: 20px'> </textarea>)</p>");
                            for (var j = 0; j < info[i].optionNumber; j++) {
                                $("#" + info[i].taskType).append("<p name=" + info[i].id + ">" + info[i].options[j].optionContent + "</p>");
                            }
                            $("#" + info[i].taskType).append("<a name=" + info[i].id + " class='btn btn-primary' onclick='delSelect(" + info[i].id + ")'>移除</a></div>");
                        } else {
                            $("#" + info[i].taskType).append("<div name='task' id=" + info[i].id + ">");
                            $("#" + info[i].taskType).append("'+info[i].title+'</br>(答案：<textarea id='answer' style='width: 80px;height: 40px'> </textarea>)");
                            $("#" + info[i].taskType).append("</div>");
                        }
                    }
                });
            },
            error: doError
        })
    }

    function displayExaminationInfoToCache() {
        $.ajax({
            url: basePath + "task/examination/displayExaminationInfoToCache?examinationInfoId=${examinationInfo.id}",
            dataType: "json",
            type: "POST",
            success: function (info) {
                for (var i = 0; i < info.length; i++) {
                    if (info[i].taskType == 1) {
                        $("#" + info[i].taskType).append("<div name='task'  id='t" + info[i].id + "'>");
                        $("#" + info[i].taskType).append("<p name=" + info[i].id + ">" + info[i].title + "(答案：<textarea id='answer' style='width: 40px;height: 20px'> </textarea>)</p>");
                        for (var j = 0; j < info[i].optionNumber; j++) {
                            $("#" + info[i].taskType).append("</br><p name=" + info[i].id + ">" + info[i].options[j].optionContent + "</p>");
                        }
                        $("#" + info[i].taskType).append("<a name=" + info[i].id + " class='btn btn-primary' onclick='delSelect(" + info[i].id + ")'>移除</a></div>");
                    } else {
                        $("#" + info[i].taskType).append("<div name='task' id=" + info[i].id + ">");
                        $("#" + info[i].taskType).append("'+info[i].title+'</br>(答案：<textarea id='answer' style='width: 80px;height: 40px'> </textarea>)");
                        $("#" + info[i].taskType).append("</div>");
                    }
                }
            },
            error: doError
        })
    }

    function addExamination(ids) {
        var ids = "";
        var name = $("#examinationName").val().trim();
        var allTask = $("[name='task']");
        for (var i = 0; i < allTask.length; i++) {
            ids += allTask[i].getAttribute("id");
        }
        ids = ids.substring(1, ids.length);
        if (ids == "") {
            $.messager.alert("提示", "试卷内容不能为空！", "", function () {
            });
            return;
        }
        if (name == "") {
            $.messager.alert("提示", "试卷名不能为空！", "", function () {
            });
            return;
        }
        $.ajax({
            url: basePath + "task/examination/addExamination?taskIds=" + ids+"&examinationName=" + name+"&type="+$("#examinationTypeId").val(),
            dataType: "json",
            type: "POST",
            success: function () {
                $.messager.alert("成功", "保存成功！", "", function () {
                })
            },
            error: doError
        })
    }

    function delSelect(id) {
        $("#t" + id).remove();
        $("[name=" + id + "]").remove();
    }

    $(function () {
        initDataGrid();
        displayExaminationInfoToCache();
        query();
    })
</script>
